package com.wink.file.center.controller;

import com.wink.file.center.config.OssServiceFactory;
import com.wink.file.center.constant.enums.FileType;
import com.wink.file.center.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;
import java.util.Objects;


/**
 * 文件控制器
 *
 * @author wink
 * @date 2020/11/17 18:11:00
 */
@Slf4j
@RestController
public class FileController {

    @Autowired
    private OssServiceFactory fileServiceFactory;

    /**
     * 上传
     * 文件上传
     * 根据fileType选择上传方式
     *
     * @param file     文件
     * @param type     oss类型
     * @param fileName 的名字
     * @return {@link Map<String, String>}
     */
    @PostMapping("/upload")
    public Map<String, String> upload(@RequestParam("file") MultipartFile file, Integer type, String fileName) {
        // 默认使用本地
        type = Objects.isNull(type) ? FileType.LOCAL.getCode() : type;
        FileService fileService = fileServiceFactory.getFileService(type);
        return fileService.upload(file, fileName);
    }


}
